package com.atguigu.study.locks;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
// cmd jconsole
// jps -l       jstack
public class DeadLock {
    static Lock lockA=new ReentrantLock();
    static Lock lockB=new ReentrantLock();
    public static void main(String[] args) {

        new Thread(()->{
          lockA.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"持有A锁=======");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lockB.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"准备获取B锁----------");
                } finally {
                    lockB.unlock();
                }
            } finally {
                lockA.unlock();
            }

        },"A").start();


        new Thread(()->{
           lockB.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"持有B锁=========");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lockA.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"准备获取A锁-------");
                } finally {
                    lockA.unlock();
                }
            } finally {
                lockB.unlock();
            }
        },"B").start();
        //ThreadPoolExecutor 线程池类

    }
}
